package com.huangli.coder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	private TextView today, y1_t, y1_c, y2_t, y2_c, y3_t, y3_c, j1_t, j1_c,
			j2_t, j2_c, j3_t, j3_c, fx, yl, zs;
	private String weeks[] = { "日","一", "二", "三", "四", "五", "六" };
	private String fangxiang[] = { "北方", "东北方", "东方", "东南方", "南方", "西南方", "西方",
			"西北方" };
	private String[][] activities = {
			{ "name", "写单元测试", "good", "写单元测试将减少出错", "bad", "写单元测试会降低你的开发效率" },
			{ "name", "洗澡", "good", "你几天没洗澡了？", "bad", "会把设计方面的灵感洗掉" },
			{ "name", "锻炼一下身体", "good", "", "bad", "能量没消耗多少，吃得却更多" },
			{ "name", "抽烟", "good", "抽烟有利于提神，增加思维敏捷", "bad", "除非你活够了，死得早点没关系" },
			{ "name", "白天上线", "good", "今天白天上线是安全的", "bad", "可能导致灾难性后果" },
			{ "name", "重构", "good", "代码质量得到提高", "bad", "你很有可能会陷入泥潭" },
			{ "name", "使用%t", "good", "你看起来更有品位", "bad", "别人会觉得你在装逼" },
			{ "name", "跳槽", "good", "该放手时就放手", "bad", "鉴于当前的经济形势，你的下一份工作未必比现在强" },
			{ "name", "招人", "good", "你遇到千里马的可能性大大增加", "bad", "你只会招到一两个混饭吃的外行" },
			{ "name", "面试", "good", "面试官今天心情很好", "bad", "面试官不爽，会拿你出气" },
			{ "name", "提交辞职申请", "good", "公司找到了一个比你更能干更便宜的家伙，巴不得你赶快滚蛋", "bad",
					"鉴于当前的经济形势，你的下一份工作未必比现在强" },
			{ "name", "申请加薪", "good", "老板今天心情很好", "bad", "公司正在考虑裁员" },
			{ "name", "晚上加班", "good", "晚上是程序员精神最好的时候", "bad", "" },
			{ "name", "在妹子面前吹牛", "good", "改善你矮穷挫的形象", "bad", "会被识破" },
			{ "name", "撸管", "good", "避免缓冲区溢出", "bad", "小撸怡情，大撸伤身，强撸灰飞烟灭" },
			{ "name", "浏览成人网站", "good", "重拾对生活的信心", "bad", "你会心神不宁" },
			{ "name", "命名变量\"%v\"", "good", "", "bad", "" },
			{ "name", "写超过%l行的方法", "good", "你的代码组织的很好，长一点没关系", "bad",
					"你的代码将混乱不堪，你自己都看不懂" },
			{ "name", "提交代码", "good", "遇到冲突的几率是最低的", "bad",
					"你遇到的一大堆冲突会让你觉得自己是不是时间穿越了" },
			{ "name", "代码复审", "good", "发现重要问题的几率大大增加", "bad",
					"你什么问题都发现不了，白白浪费时间" },
			{ "name", "开会", "good", "写代码之余放松一下打个盹，有益健康", "bad", "你会被扣屎盆子背黑锅" },
			{ "name", "打DOTA", "good", "你将有如神助", "bad", "你会被虐的很惨" },
			{ "name", "晚上上线", "good", "晚上是程序员精神最好的时候", "bad", "你白天已经筋疲力尽了" },
			{ "name", "修复BUG", "good", "你今天对BUG的嗅觉大大提高", "bad",
					"新产生的BUG将比修复的更多" },
			{ "name", "设计评审", "good", "设计评审会议将变成头脑风暴", "bad", "人人筋疲力尽，评审就这么过了" },
			{ "name", "需求评审", "good", "", "bad", "" },
			{ "name", "上微博", "good", "今天发生的事不能错过", "bad", "会被老板看到" },
			{ "name", "上AB站", "good", "还需要理由吗？", "bad", "会被老板看到" } };

	private String drinks[] = { "水", "茶", "红茶", "绿茶", "咖啡", "奶茶", "可乐", "牛奶",
			"豆奶", "果汁", "果味汽水", "苏打水", "运动饮料", "酸奶", "酒" };
	private String names[] = { "jieguo", "huodong", "pay", "expire",
			"zhangdan", "every", "free", "i1", "a", "virtual", "ad", "spider",
			"mima", "pass", "ui" };
	private String tools[] = { "Eclipse写程序", "MSOffice写文档", "记事本写程序",
			"Windows8", "Linux", "MacOS", "IE", "Android设备", "iOS设备" };
	private List<Map<String, Object>> list = null;
	private int iday;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityList.activityList.add(this);
		setContentView(R.layout.main);
		init();
		setDate();
		getData();
		pickTodaysLuck();
	}

	/**
	 * init()方法,初始化UI
	 */
	private void init() {
		today = (TextView) findViewById(R.id.today);
		y1_t = (TextView) findViewById(R.id.yi1_title);
		y1_c = (TextView) findViewById(R.id.yi1_content);
		y2_t = (TextView) findViewById(R.id.yi2_title);
		y2_c = (TextView) findViewById(R.id.yi2_content);
		y3_t = (TextView) findViewById(R.id.yi3_title);
		y3_c = (TextView) findViewById(R.id.yi3_content);

		j1_t = (TextView) findViewById(R.id.ji1_title);
		j1_c = (TextView) findViewById(R.id.ji1_content);
		j2_t = (TextView) findViewById(R.id.ji2_title);
		j2_c = (TextView) findViewById(R.id.ji2_content);
		j3_t = (TextView) findViewById(R.id.ji3_title);
		j3_c = (TextView) findViewById(R.id.ji3_content);
		fx = (TextView) findViewById(R.id.fangxiang);
		yl = (TextView) findViewById(R.id.yinliao);
		zs = (TextView) findViewById(R.id.zhishu);
	}

	/**
	 * 初始化日期
	 */
	private void setDate() {
		Calendar c = Calendar.getInstance();
		today.setText(new StringBuilder().append("今天是")
				.append(c.get(Calendar.YEAR)).append("年")
				.append((c.get(Calendar.MONTH)+1)).append("月")
				.append((c.get(Calendar.DAY_OF_MONTH))).append("日")
				.append("星期")
				.append(weeks[c.get(Calendar.DAY_OF_WEEK)-1]));
		
				System.out.println("qqqqqqqq"+c.get(Calendar.DAY_OF_WEEK));
				
		iday = c.get(Calendar.YEAR) * 10000 + (c.get(Calendar.MONTH) + 1) * 100
				+ c.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 加载数据 size=28
	 */
	private void getData() {
		list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < activities.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(activities[i][0], activities[i][1]);
			map.put(activities[i][2], activities[i][3]);
			map.put(activities[i][4], activities[i][5]);
			list.add(map);
		}
	}

	/**
	 * 今日运势
	 */

	private void pickTodaysLuck() {
		int godNum = 3;
		int badNum = 3;
		List<String[]> eventArr = pickRandom(godNum + badNum);
		List<String[]> list = new ArrayList<String[]>();
		for (int i = 0; i < godNum; i++) {

			list.add(eventArr.get(i));
		}
		for (int i = godNum; i < badNum + godNum; i++) {
			list.add(eventArr.get(i));
		}
		addToGood(list);
		addToBad(list);
		fx.setText(fangxiang[random(iday, 2) % fangxiang.length]);
		yl.setText(drinks[random(iday, 5) % drinks.length]);
		zs.setText(String.valueOf(random(iday, 6) % 50 / 10.0));

	}

	/*
	 * 本程序中的“随机”都是伪随机概念，以当前的天为种子。
	 */
	private int random(int day, int index) {
		int n = day % 11117;
		for (int i = 0; i < 100 + index; i++) {
			n = n * n + 11117;
			n = n % 11117; // 11117 是个质数
		}
		return n;
	}

	private List<String[]> pickRandom(int size) {
		List<String[]> list = new ArrayList<String[]>();
		// Random rnd = new Random();
		int[] arr = new int[size];
		Random rnd = new Random(iday);
		for (int i = 0; i < size; i++) {
			/*
			 * 生成不重不随机数
			 */
			arr[i] = rnd.nextInt(28);
			// arr[i]=random(iday, i)%8;

			for (int j = 0; j < i; j++) {
				if (arr[j] == arr[i]) {
					i--;
					break;
				}
			}
		}
		for (int k = 0; k < size; k++) {
			System.out.println(arr[k]);
			list.add(parse(activities[arr[k]]));
		}
		return list;
	}

	/**
	 * 解析占位符并替换成随机内容
	 */
	public String[] parse(String[] event) {
		String[] result = { "name", event[1], "good", event[3], "bad", event[5] };

		if (result[1].indexOf("%v") != -1) {
			result[1] = result[1].replace("%v", names[random(iday, 12)
					% names.length]);
		}

		if (result[1].indexOf("%t") != -1) {
			result[1] = result[1].replace("%t", tools[random(iday, 12)
					% tools.length]);
		}

		if (result[1].indexOf("%l") != -1) {
			result[1] = result[1].replace("%l",
					String.valueOf(random(iday, 12) % 247 + 30));
		}

		return result;
	}

	private void addToGood(List<String[]> list) {
		y1_t.setText(list.get(0)[1]);
		y1_c.setText(list.get(0)[3]);

		y2_t.setText(list.get(1)[1]);
		y2_c.setText(list.get(1)[3]);

		y3_t.setText(list.get(2)[1]);
		y3_c.setText(list.get(2)[3]);

	}

	private void addToBad(List<String[]> list) {
		j1_t.setText(list.get(3)[1]);
		j1_c.setText(list.get(3)[5]);

		j2_t.setText(list.get(4)[1]);
		j2_c.setText(list.get(4)[5]);

		j3_t.setText(list.get(5)[1]);
		j3_c.setText(list.get(5)[5]);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, 1, 1, "退出");
		menu.add(0, 2, 2, "关于");

		return super.onCreateOptionsMenu(menu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == 1) {
			ActivityList.exitClient(MainActivity.this);
		} else if (item.getItemId() == 2) {
			startActivity(new Intent(MainActivity.this, About.class));
		}
		return true;
	}

		// 退出提示
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			MainActivity.this.showTips();
			return false;
		}
		return false;
	}

	private void showTips() {
		// 创建对话框
		AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
				.setTitle("退出程序").setMessage("是否退出程序")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						ActivityList.exitClient(MainActivity.this);
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				}).create(); 
		// 显示对话框
		alertDialog.show(); 
	}

}